/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.model.HealthSpringCenter;
import cc.altius.model.Partner;
import cc.altius.model.PartnerDetail;
import cc.altius.model.PartnerType;
import cc.altius.service.CenterService;
import cc.altius.service.PartnerService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author jitendra
 */
@Controller
public class PartnerController {

    @Autowired
    PartnerService patnerService;
    @Autowired
    CenterService centerService;

    @ModelAttribute("HealthSpringCenter")
    public List<HealthSpringCenter> populateCityList() {
        return this.centerService.getCenterList();
    }

    @ModelAttribute("PartnerDetail")
    public List<PartnerDetail> getPartnerDetailList() {
        return this.patnerService.getPartnerDetailList();
    }

    @ModelAttribute("PartnerType")
    public List<PartnerType> getPartnerTypeList() {
        return this.patnerService.getPartnerTypeList();
    }

    @RequestMapping(value = "partner/addPartner.htm", method = RequestMethod.GET)
    public String showAddPartner(@ModelAttribute("partner") Partner partner) {
        return "partner/addPartner";
    }

    @RequestMapping(value = "partner/addPartner.htm", method = RequestMethod.POST)
    public String AddPartner(@ModelAttribute("partner") Partner partner, SessionStatus sessionStatus) {
        this.patnerService.addPartner(partner);
        sessionStatus.setComplete();
        return "redirect:listPartner.htm";
    }

    @RequestMapping(value = "partner/listPartner.htm")
    public String showListPartner(@ModelAttribute("partner") Partner partner, HttpServletRequest request, ModelMap model) {
        int centerId = ServletRequestUtils.getIntParameter(request, "HealthSpringCenter.centerId", 0);
        int partnerTypeid = ServletRequestUtils.getIntParameter(request, "PartnerType.partnerTypeId", 0);
        List<Partner> partnerList = this.patnerService.getPartnerList(centerId, partnerTypeid);
        model.addAttribute("partnerList", partnerList);
        return "partner/listPartner";
    }

    @RequestMapping(value = "partner/addPartnerDetail.htm", method = RequestMethod.GET)
    public String showAddPartnerDetail(@ModelAttribute("partnerDetail") PartnerDetail partnerDetail) {
        return "partner/addPartnerDetail";
    }

    @RequestMapping(value = "partner/addPartnerDetail.htm", method = RequestMethod.POST)
    public String AddPartnerDetail(@ModelAttribute("partnerDetail") PartnerDetail partnerDetail) {
        this.patnerService.addPartnerDetail(partnerDetail);
        return "redirect:addPartner.htm?partnerDetail.partnerDetailId="+partnerDetail.getPartnerDetailId();
    }

    //
    @RequestMapping(value = "partner/editPartnerDetail.htm", method = RequestMethod.GET)
    public String showEditPartnerDetail(ModelMap modelmap, HttpServletRequest request, SessionStatus sessionStatus) {
        int partnerDetailId = ServletRequestUtils.getIntParameter(request, "partnerDetailId", 0);
        int partnerID = ServletRequestUtils.getIntParameter(request, "partnerID", 0);
        PartnerDetail partnerDetail = this.patnerService.getPartnerDetailById(partnerDetailId);
        modelmap.addAttribute("partnerDetail", partnerDetail);
        modelmap.addAttribute("partnerID",partnerID);
        sessionStatus.setComplete();
        return "partner/editPartnerDetail";
    }

    @RequestMapping(value = "partner/editPartnerDetail.htm", method = RequestMethod.POST)
    public String UpdatePartnerDetail(@ModelAttribute("partnerDetail") PartnerDetail partnerDetail,HttpServletRequest request, SessionStatus sessionStatus) {
        int partnerID=ServletRequestUtils.getIntParameter(request, "partnerID",0);
        this.patnerService.updatePartnerDetail(partnerDetail);
        sessionStatus.setComplete();
        return "redirect:editPartner.htm?partnerID="+partnerID;
    }
    //
    @RequestMapping(value = "partner/editPartner.htm", method = RequestMethod.GET)
    public String showEditPartner(ModelMap modelmap, HttpServletRequest request, SessionStatus sessionStatus) {
        int partnerID = ServletRequestUtils.getIntParameter(request, "partnerID", 0);
        Partner partner = this.patnerService.getPartnerById(partnerID);
        modelmap.addAttribute("partner", partner);
        sessionStatus.setComplete();
        return "partner/editPartner";
    }

    @RequestMapping(value = "partner/editPartner.htm", method = RequestMethod.POST)
    public String UpdatePartner(@ModelAttribute("partner") Partner partner, SessionStatus sessionStatus) {
        this.patnerService.updatePartner(partner);
        sessionStatus.setComplete();
        return "redirect:listPartner.htm";
    }
}
